package com.thread.thread03;
/**
 * 对象锁的同步和异步问题
 *
 */
public class MyObject {

	public  synchronized void m1() throws InterruptedException {
		System.out.println(Thread.currentThread().getName());
		Thread.sleep(4000L);
	}
	public synchronized  void m2()  {
		System.out.println(Thread.currentThread().getName());
	
	}
	/**
	 * 分析：
	 * t1线程先持有object对象的Lock锁，t2线程可以以异步的方式调用对象中的非synchronized修饰的方法
	 * t1线程先持有object对象的Lock锁，t2线程如果在这个时候调用对象中的同步（synchronized）方法则需等待，也就是同步
	 */
	public static void main(String[] args) {
		MyObject myObject = new MyObject();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
					try {
						myObject.m1();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}, "t1");
		
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
					myObject.m2();
			}
		}, "t2");
		t1.start();
		t2.start();
	}

}
